import { StandardMaterial } from '@babylonjs/core/Materials/standardMaterial'
import { Color3, Color4, Vector3 } from '@babylonjs/core/Maths'
import { MeshBuilder } from '@babylonjs/core/Meshes/meshBuilder'
import type { Scene } from '@babylonjs/core/scene'

export const createGround = (scene: Scene) => {
  const ground = MeshBuilder.CreateGround(
    'mapGround',
    {
      width: 500,
      height: 500,
      subdivisions: 100,
      updatable: true,
    },
    scene
  )
  ground.position.y = -1
  const groundMaterial = new StandardMaterial('groundMaterial', scene)
  groundMaterial.diffuseColor = new Color3(72 / 255, 180 / 255, 200 / 255)
  groundMaterial.alpha = 0.6
  // groundMaterial.diffuseTexture = new Texture("/src/assets/textures/grass.jpeg", scene);
  ground.material = groundMaterial
  return ground
}

// 网格线
const lineGround = (scene: Scene) => {
  const colors = [new Color4(0.8, 0.8, 0.8, 1), new Color4(0.9, 0.9, 0.9, 1)]
  const lineHeight = -0.1

  const range = 40
  const segCount = 20
  // 每个网格线之间的间隔
  const spaceInterval = range / segCount

  for (let i = -range; i <= range; i += spaceInterval) {
    const points = [
      new Vector3(-range, lineHeight, i),
      new Vector3(range, lineHeight, i),
    ]

    MeshBuilder.CreateLines('lineZ_' + i, { points, colors }, scene)
  }

  for (let i = -range; i <= range; i += spaceInterval) {
    const points = [
      new Vector3(i, lineHeight, -range),
      new Vector3(i, lineHeight, range),
    ]

    MeshBuilder.CreateLines('lineX_' + i, { points, colors }, scene)
  }
}
